package Power_Asisst.main;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;

public class TCP_Services extends Activity {

	Untils SVUntils= new Untils();
	String SERVERIP=SVUntils.getLocalIPAddress();
	public Handler handler = new Handler();

	private ServerSocket serverSocket;
	public static int SERVERPORT=8080;
	public  String result="None";
	TextView serverStatus = (TextView) findViewById(R.id.textViewReceive);
	public void testShow(String input)
	{
		String[] result=input.split(",");
		new AlertDialog.Builder( this )
	   	.setTitle( "Title" )
	   	.setMessage( "Đã Kết Nối Với Client"+result[0]+" IP:"+result[1] )
	   	.setNegativeButton( "Accept", new DialogInterface.OnClickListener() {
	   		public void onClick(DialogInterface dialog, int which) {
	   			Log.d( "AlertDialog", "Negative" );
	   		}
	   	} )
	   	.show();
	}
	public class ServerThread implements Runnable {
		
	    public void run() {
	        try {
	            if (SERVERIP != null) {
	            handler.post(new Runnable() {
	                    @Override
	                    public void run() {
	                 serverStatus.setText("Listening on IP: " + SERVERIP);
	                  }
	                });
	                serverSocket = new ServerSocket(SERVERPORT);
	                while (true) {
	                	serverStatus.setText("Listening on IP: " + SERVERIP);
	                    // listen for incoming clients
	                    Socket client = serverSocket.accept();
	                    handler.post(new Runnable() {
	                        @Override
	                        public void run() {
	                            serverStatus.setText("Connected.");
	                        }
	                    });

	                    try {
	                        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
	                        String line = null;
	                        while ((line = in.readLine()) != null) {
	                            Log.d("ServerActivity", line);
	                            serverStatus.setText(line);
	                            result=line.toString();
	                            handler.post(new Runnable() {
	                                @Override
	                            public void run() {
	                                    // set something
	                                    
	                                }
	                            });
	                        }
	                    break;
	                    } catch (Exception e) {
	                        handler.post(new Runnable() {
	                            @Override
	                            public void run() {
	                                serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones.");
	                            }
	                        });
	                        e.printStackTrace();
	                    }
	                }
	            } else {
	                handler.post(new Runnable() {
	                    @Override
	                    public void run() {
	                        serverStatus.setText("Couldn't detect internet connection.");
	                    }
	                });
	            }
	        } catch (Exception e) {
	            handler.post(new Runnable() {
	                @Override
	                public void run() {
	                    serverStatus.setText("Error");
	                }
	            });
	            e.printStackTrace();
	        }
	        serverStatus.setText(result);
	        
	       
	    }
	    
	}
	
	    	  
	    

}

